﻿using System.Collections.Generic;
using System.Diagnostics;
using Flatland.GeneticProgramming.CoreTypes;
using Flatland.Utilities;

namespace Flatland.GeneticProgramming.Selections {
    class Tournament : ISelection {

        /// <summary>
        /// Perform tournament selection.
        /// </summary>
        /// <param name="reports"></param>
        /// <returns></returns>
        public List<Report> Select(List<Report> reports) {
            Debug.Assert(reports != null, "Argument was null", "reports in Selection was null");
            int count = reports.Count;
            List<Report> selected = new List<Report>(count);
            for (int i = 0; i < count; i++) {
                Report a = reports[Utils.Random.Next(count)];
                Report b = reports[Utils.Random.Next(count)];
                selected.Add(a.Fitness <= b.Fitness ? (Report)b.Clone() : (Report)a.Clone());
            }
            return selected;
        }
    }
}
